package com.example.yichuangyun.ui.fragment;

import android.os.Bundle;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.FragmentDetailsFollowRecordBinding;
import com.example.yichuangyun.databinding.FragmentDetailsOrderBinding;
import com.example.yichuangyun.http.HttpRespond;
import com.example.yichuangyun.http.MyObserver;
import com.example.yichuangyun.http.RetrofitHelper;
import com.example.yichuangyun.model.business.OrderModel;
import com.example.yichuangyun.mvvm.base.BaseFragment;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.ui.adapter.OrderAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 商机详情-订单记录
 */
public class DetailsOrderRecordFragment extends BaseFragment<FragmentDetailsOrderBinding, BaseViewModel> {
    private String businessId;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void lazyLoadData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            businessId = bundle.getString(Constant.businessIdKey);
        }
        checkButtonOrTab();
    }

    /**
     * 订单记录
     */
    private void getOrderRecord() {
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().getOrderList(businessId), new MyObserver<HttpRespond<List<OrderModel>>>() {
            @Override
            protected void onSuccess(HttpRespond<List<OrderModel>> listHttpRespond) {
                if (Constant.SUCCESS == listHttpRespond.code) {
                    if (listHttpRespond.data != null) {
                        List<OrderModel> orderData = listHttpRespond.data;
                        OrderAdapter orderAdapter = new OrderAdapter(mContext, orderData);
                        binding.recyclerView.setAdapter(orderAdapter);
                    }
                }
            }
        });
    }

    public static DetailsOrderRecordFragment newInstance(String businessId) {
        Bundle bundle = new Bundle();
        bundle.putString(Constant.businessIdKey, businessId);
        DetailsOrderRecordFragment orderRecordFragment = new DetailsOrderRecordFragment();
        orderRecordFragment.setArguments(bundle);
        return orderRecordFragment;
    }

    /**
     * 检测是否有按钮权限
     */
    private void checkButtonOrTab() {
        JSONObject requestDate = new JSONObject();
        try {
            // 1、跟进记录 2、通话记录 3、订单记录 4、添加跟进 5、编辑信息 6、设置共享人 7、丢单 8、商机详情
            requestDate.put("type", 1);
            // 商机类型  0、我创建的 1、部门创建的 2、我跟进的 3、部门跟进的 4、共享给我的 5、共享给部门的
            requestDate.put("businessType", Constant.businessType);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), requestDate.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().checkButtonOrTab(requestBody), new MyObserver<HttpRespond<Boolean>>() {
            @Override
            protected void onSuccess(HttpRespond<Boolean> booleanHttpRespond) {
                if (Constant.SUCCESS == booleanHttpRespond.code) {
                    if (booleanHttpRespond.data) {
                        getOrderRecord();
                    }
                }
            }
        });
    }
}
